#!/bin/bash
# HAI服务器快速管理脚本

SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
MANAGE_SCRIPT="$SCRIPT_DIR/manage_servers.py"

# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color

# 显示帮助信息
show_help() {
    echo -e "${BLUE}HAI服务器快速管理脚本${NC}"
    echo "用法: $0 [命令]"
    echo ""
    echo "命令:"
    echo "  status      - 显示系统状态"
    echo "  keep-one    - 只保留1个服务器"
    echo "  destroy-all - 销毁所有服务器"
    echo "  scale N     - 缩放到N个服务器"
    echo "  help        - 显示此帮助信息"
    echo ""
    echo "示例:"
    echo "  $0 status"
    echo "  $0 keep-one"
    echo "  $0 scale 5"
    echo "  $0 destroy-all"
}

# 检查Python脚本是否存在
if [ ! -f "$MANAGE_SCRIPT" ]; then
    echo -e "${RED}❌ 管理脚本不存在: $MANAGE_SCRIPT${NC}"
    exit 1
fi

# 检查参数
if [ $# -eq 0 ]; then
    show_help
    exit 1
fi

# 执行命令
case "$1" in
    "status")
        echo -e "${BLUE}📊 获取系统状态...${NC}"
        python3 "$MANAGE_SCRIPT" status
        ;;
    "keep-one")
        echo -e "${YELLOW}🎯 只保留1个服务器...${NC}"
        python3 "$MANAGE_SCRIPT" keep-one
        ;;
    "destroy-all")
        echo -e "${RED}🗑️ 销毁所有服务器...${NC}"
        python3 "$MANAGE_SCRIPT" destroy-all
        ;;
    "scale")
        if [ -z "$2" ] || ! [[ "$2" =~ ^[0-9]+$ ]]; then
            echo -e "${RED}❌ 请提供有效的服务器数量${NC}"
            echo "用法: $0 scale N"
            exit 1
        fi
        echo -e "${YELLOW}🎯 缩放到 $2 个服务器...${NC}"
        python3 "$MANAGE_SCRIPT" scale --target "$2"
        ;;
    "help"|"-h"|"--help")
        show_help
        ;;
    *)
        echo -e "${RED}❌ 未知命令: $1${NC}"
        show_help
        exit 1
        ;;
esac
